package m202401.d05;

import java.util.Stack;

/**
 * @@author: 爱做梦的锤子
 * @date: 2024/1/5 09:44
 */
//150. 逆波兰表达式求值
public class Q150 {
    public int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack<>();
        for (String token : tokens) {
            if (!token.equals("+") && !token.equals("*") && !token.equals("/") && !token.equals("-")) {
                stack.push(Integer.parseInt(token));
                continue;
            }
            Integer i2 = stack.pop();
            Integer i1 = stack.pop();
            if (token.equals("+")) {
                stack.push(i1 + i2);
            } else if (token.equals("-")) {
                stack.push(i1 - i2);
            } else if (token.equals("*")) {
                stack.push(i1 * i2);
            } else {
                stack.push(i1 / i2);
            }
        }
        return stack.pop();
    }
}
